home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Applications / ircle 1.5.1 / source / ircle sources / IRCaux.p < prev    next >
Encoding:
Text File  |  1993-10-12  |  7.0 KB  |  341 lines  |  [TEXT/PJMM]

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: IRCaux    }
  3. {    Copyright © 1992 Olaf Titz (s_titz@ira.uka.de)    }
  4.  
  5. {    This program is free software; you can redistribute it and/or modify    }
  6. {    it under the terms of the GNU General Public License as published by    }
  7. {    the Free Software Foundation; either version 2 of the License, or    }
  8. {    (at your option) any later version.    }
  9.  
  10. {    This program is distributed in the hope that it will be useful,    }
  11. {    but WITHOUT ANY WARRANTY; without even the implied warranty of    }
  12. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    }
  13. {    GNU General Public License for more details.    }
  14.  
  15. {    You should have received a copy of the GNU General Public License    }
  16. {    along with this program; if not, write to the Free Software    }
  17. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.    }
  18.  
  19. unit IRCaux;
  20. { utilities }
  21.  
  22. interface
  23. uses
  24.     TCPTypes, TCPStuff, TCPConnections, ApplBase, InputLine, {}
  25.     MiscGlue, MsgWindows, IRCGlobals;
  26.  
  27. var
  28.     Watch: CursHandle;
  29.  
  30. procedure ServerOK (status: connectionEvent);
  31. { Call this with the result of TCP functions }
  32.  
  33. procedure PutLine (var s: string);
  34. { Send a line to the server }
  35.  
  36. function IsChannel (var s: string): boolean;
  37. { is it a valid channel name? }
  38.  
  39. procedure MakeChannel (var s: string);
  40. { insert # to make a channel name }
  41.  
  42. procedure NextArg (var from, arg: string);
  43. { get next arg out of 'from' into 'arg' }
  44.  
  45. procedure OpenConnection;
  46. { open server connection }
  47.  
  48. procedure UpdateStatusLine;
  49. { Draw the IRC status line }
  50.  
  51. procedure AdjustFontMenu;
  52. { Set the font menu check marks }
  53.  
  54. function ValidPrefs: boolean;
  55. { check whether all settings are correct }
  56.  
  57. implementation
  58.  
  59. type
  60.     str8 = string[8];
  61.  
  62. var
  63.     ip: longint;
  64.  
  65. procedure ServerOK (status: connectionEvent);
  66.     var
  67.         a, n: integer;
  68.         s: Str255;
  69.     begin
  70.         case status of
  71.             C_SearchFailed: 
  72.                 begin
  73.                 a := S_OFFLINE;
  74.                 n := E_SFAILED;
  75.             end;
  76.             C_NameSearchFailed: 
  77.                 begin
  78.                 a := S_OFFLINE;
  79.                 n := E_NSFAILED;
  80.             end;
  81.             C_FailedToOpen: 
  82.                 begin
  83.                 a := S_OFFLINE;
  84.                 n := E_OFAILED;
  85.             end;
  86.             C_Closing: 
  87.                 begin
  88.                 a := S_OFFLINE;
  89.                 n := E_CLOSING;
  90.             end;
  91.             C_Closed: 
  92.                 begin
  93.                 a := S_OFFLINE;
  94.                 n := 0; { this alert is redundant }
  95.             end;
  96.             otherwise
  97.                 begin
  98.                 a := serverStatus;
  99.                 n := 0;
  100.             end;
  101.         end;
  102.         serverStatus := a;
  103.         if n <> 0 then begin
  104.             GetIndString(s, 256, n);
  105.             ParamText(s, '', '', '');
  106.             InitCursor;
  107.             n := Alert(A_SSTAT, nil);
  108.         end;
  109.         UpdateStatusLine;
  110.     end;
  111.  
  112. procedure PutLine (var s: string);
  113.     var
  114.         i, n, oe: integer;
  115.         p: TCPConnectionPtr;
  116.     begin
  117.         n := length(s);
  118.         for i := 1 to n do
  119.             s[i] := ISOEncode^^[s[i]];
  120.         s[n + 1] := chr(10);
  121.         GetConnectionTCPC(sSocket, p);
  122.         i := TCPSendAsync(p, @s[1], n + 1, false, @oe);
  123.         if i <> 0 then
  124.             serverStatus := i
  125.         else begin
  126.             repeat
  127.                 ApplRun
  128.             until oe <> inProgress;
  129.             serverStatus := oe;
  130.             if oe <> 0 then
  131.                 UpdateStatusLine;
  132.         end;
  133.     end;
  134.  
  135. function IsChannel (var s: string): boolean;
  136.     begin
  137.         IsChannel := (s[1] = '#') or (s[1] = '&'); { RFC 1459 }
  138.     end;
  139.  
  140. procedure MakeChannel (var s: string);
  141.     begin
  142.         if s[1] <> '#' then
  143.             insert('#', s, 1);
  144.     end;
  145.  
  146. procedure NextArg (var from, arg: string);
  147.     var
  148.         i: integer;
  149.     begin
  150.         i := pos(' ', from);
  151.         if i = 0 then begin
  152.             arg := from;
  153.             from := ''
  154.         end
  155.         else begin
  156.             arg := copy(from, 1, i - 1);
  157.             while i < length(from) do begin
  158.                 if (from[i + 1] <> ':') and (from[i + 1] <> ' ') then
  159.                     leave;
  160.                 i := i + 1;
  161.             end;
  162.             delete(from, 1, i);
  163.         end
  164.     end;
  165.  
  166.  
  167. function watchFound (var e: EventRecord): boolean;
  168.     var
  169.         c: CEPtr;
  170.     begin
  171.         c := CEPtr(e.message);
  172.         if c^.connection <> sSocket then begin
  173.             watchFound := false;
  174.             exit(watchFound)
  175.         end
  176.         else
  177.             watchFound := true;
  178.         if c^.event = C_Found then
  179.             ip := c^.value
  180.         else begin
  181.             ip := -1;
  182.             ServerOk(c^.event);
  183.         end;
  184.     end;
  185.  
  186. function watchOpen (var e: EventRecord): boolean;
  187.     var
  188.         c: CEPtr;
  189.     begin
  190.         c := CEPtr(e.message);
  191.         if c^.connection <> sSocket then begin
  192.             watchOpen := false;
  193.             exit(watchOpen)
  194.         end
  195.         else
  196.             watchOpen := true;
  197.         if c^.event = C_Established then
  198.             ip := 1
  199.         else begin
  200.             ip := -1;
  201.             ServerOk(c^.event);
  202.         end;
  203.     end;
  204.  
  205. procedure WaitEvent (p: ProcPtr);
  206.     var
  207.         i: integer;
  208.     begin
  209.         i := ApplTask(p, TCPMsg);
  210.         ip := 0;
  211.         repeat
  212.             ApplRun;
  213.             if flushing then begin
  214.                 flushing := false;
  215.                 ip := -1
  216.             end
  217.         until ip <> 0;
  218.         ApplUNtask(i);
  219.     end;
  220.  
  221. procedure OpenConnection;
  222.     var
  223.         e: integer;
  224.     begin
  225.         if (serverStatus <> S_CONN) and ValidPrefs then begin
  226.             CurrentNick := default^^.nick;
  227.             UserRegistered := false;
  228.             serverStatus := S_LOOKUP;
  229.             flushing := false;
  230.             UpdateStatusLine;
  231.             SetCursor(Watch^^);
  232.             e := FindAddress(sSocket, default^^.server, nil);
  233.             if e = 0 then begin
  234.                 ip := 0;
  235.                 WaitEvent(@watchFound);
  236.                 if ip <> -1 then begin
  237.                     serverStatus := S_OPENING;
  238.                     UpdateStatusLine;
  239.                     e := NewActiveConnection(sSocket, 8192, ip, default^^.port, nil);
  240.                     if e = 0 then begin
  241.                         WaitEvent(@watchOpen);
  242.                         if ip <> -1 then begin
  243.                             serverStatus := S_CONN;
  244.                         end
  245.                         else
  246.                             serverOk(C_FailedToOpen)
  247.                     end
  248.                     else
  249.                         serverOk(C_FailedToOpen)
  250.                 end
  251.             end
  252.             else
  253.                 serverOk(C_SearchFailed)
  254.         end;
  255.         UpdateStatusLine;
  256.         InitCursor;
  257.     end;
  258.  
  259.  
  260. function two (n: integer): str8;
  261.     var
  262.         s: str8;
  263.     begin
  264.         s := stringof(n + 100 : 3);
  265.         two := copy(s, 2, 2)
  266.     end;
  267.  
  268. procedure UpdateStatusLine;
  269.     var
  270.         s: string[80];
  271.         s0: string[40];
  272.         sa, s1, s2, s3: string[10];
  273.         d: DateTimeRec;
  274.     begin
  275.         case serverStatus of
  276.             S_OFFLINE: 
  277.                 s := '(Offline)';
  278.             S_LOOKUP: 
  279.                 s := '(Address lookup)';
  280.             S_OPENING: 
  281.                 s := '(opening)';
  282.             S_CONN: 
  283.                 s := CurrentServer;
  284.             S_CLOSING, connectionClosing: 
  285.                 s := '(closing)';
  286.             otherwise
  287.                 s := stringof('Err(', serverStatus : 1, ')');
  288.         end;
  289.         if (serverStatus = S_CONN) or (serverStatus = S_OPENING) or (serverStatus = S_LOOKUP) or (serverStatus = S_CLOSING) then
  290.             DisableItem(GetMHandle(fileMenu), M_F_OPEN)
  291.         else
  292.             EnableItem(GetMHandle(fileMenu), M_F_OPEN);
  293.         if IsAway then
  294.             sa := ' (away)   '
  295.         else
  296.             sa := 'talking to';
  297.         s0 := CurrentTarget;
  298.         if s0 = '' then
  299.             s0 := '(nobody)';
  300.         s0 := concat(s0, '               ');
  301.         if logging then
  302.             s1 := 'Log'
  303.         else
  304.             s1 := '';
  305.         if flushing then
  306.             s2 := 'Flsh'
  307.         else
  308.             s2 := '';
  309.         if NFT > 0 then
  310.             s3 := stringof('FT(', NFT : 1, ')')
  311.         else
  312.             s3 := '';
  313.         GetTime(d);
  314.         s := stringof(CurrentNick : 10, ' talking to', copy(s0, 1, 12) : 13, s : 18, s1 : 4, s2 : 5, s3 : 8, '  ', two(d.hour), ':', two(d.minute), ':', two(d.second));
  315.         StatusLine(s);
  316.     end;
  317.  
  318. procedure AdjustFontMenu;
  319.     var
  320.         m: MenuHandle;
  321.         i, j: integer;
  322.         s: str255;
  323.     begin
  324.         m := GetMHandle(M_FONT);
  325.         CheckItem(m, 1, (MWdefaultSize = 9));
  326.         CheckItem(m, 2, (MWdefaultSize = 10));
  327.         CheckItem(m, 3, (MWdefaultSize = 12));
  328.         CheckItem(m, 4, (MWdefaultSize = 14));
  329.         for i := 6 to CountMItems(m) do begin
  330.             GetItem(m, i, s);
  331.             GetFNum(s, j);
  332.             CheckItem(m, i, (j = MWdefaultFont));
  333.         end;
  334.     end;
  335.  
  336. function ValidPrefs: boolean;
  337.     begin
  338.         ValidPrefs := (default^^.server <> '') and (default^^.port <> 0) and (default^^.nick <> '') and (default^^.username <> '') and (default^^.userLoginname <> '')
  339.     end;
  340.  
  341. end.